package com.demo.apparel.mapper.web;

import com.demo.apparel.entity.Menu;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MenuMapper {
    @Select("SELECT id, parent_id AS parentId, path, name, component, redirect, "
            + "icon, title, is_link AS isLink, is_hide AS isHide, "
            + "is_full AS isFull, is_affix AS isAffix, is_keep_alive AS isKeepAlive "
            + "FROM menu WHERE parent_id IS NULL")
    @Results(id="menuResultMap", value = {
            @Result(column="id", property="id", id=true),
            @Result(column="parentId", property="parentId"),
            @Result(column="path", property="path"),
            @Result(column="name", property="name"),
            @Result(column="component", property="component"),
            @Result(column="redirect", property="redirect"),
            @Result(column="icon", property="meta.icon"),
            @Result(column="title", property="meta.title"),
            @Result(column="isLink", property="meta.isLink"),
            @Result(column="isHide", property="meta.isHide"),
            @Result(column="isFull", property="meta.isFull"),
            @Result(column="isAffix", property="meta.isAffix"),
            @Result(column="isKeepAlive", property="meta.isKeepAlive"),
            @Result(property="children", column="id",
                    many=@Many(select="selectByParentId"))
    })
    List<Menu> selectRootMenus();

    @Select("SELECT id, parent_id AS parentId, path, name, component, redirect, "
            + "icon, title, is_link AS isLink, is_hide AS isHide, "
            + "is_full AS isFull, is_affix AS isAffix, is_keep_alive AS isKeepAlive "
            + "FROM menu WHERE parent_id = #{parentId}")
    @ResultMap("menuResultMap")
    List<Menu> selectByParentId(@Param("parentId") Integer parentId);

    @Insert("INSERT INTO menu (parent_id, path, name, component, redirect, icon, title, is_link, is_hide, is_full, is_affix, is_keep_alive) " +
            "VALUES (#{parentId}, #{path}, #{name}, #{component}, #{redirect}, #{meta.icon}, #{meta.title}, #{meta.isLink}, #{meta.isHide}, " +
            "#{meta.isFull}, #{meta.isAffix}, #{meta.isKeepAlive})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertMenu(Menu menu);

    @Update("UPDATE menu SET parent_id = #{parentId}, path = #{path}, name = #{name}, component = #{component}, " +
            "redirect = #{redirect}, icon = #{meta.icon}, title = #{meta.title}, is_link = #{meta.isLink}, " +
            "is_hide = #{meta.isHide}, is_full = #{meta.isFull}, is_affix = #{meta.isAffix}, is_keep_alive = #{meta.isKeepAlive} " +
            "WHERE id = #{id}")
    int updateMenu(Menu menu);

    @Delete("DELETE FROM menu WHERE id = #{id}")
    int deleteById(@Param("id") Integer id);

}
